package com.mathassistant.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mathassistant.client.service.TopicService;
import com.mathassistant.server.domain.Topic;
import com.mathassistant.shared.TopicDTO;

@SuppressWarnings("serial")
public class TopicServiceImpl extends RemoteServiceServlet implements TopicService {

	@Override
	public void createTopic(TopicDTO topic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic top = new Topic(topic);
			pm.makePersistent(top);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} 
		finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<TopicDTO> getTopics() {
		// TODO Auto-generated method stub
		ArrayList<TopicDTO> list = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Query q = pm.newQuery(Topic.class);
			List<Topic> result = (List<Topic>) q.execute();
			if(!result.isEmpty()){
				list = new ArrayList<TopicDTO>();
				for(Topic t : result){
					list.add(t.toDTO());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			pm.close();
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<TopicDTO> getTopic(String type) {
		// TODO Auto-generated method stub
		ArrayList<TopicDTO> list = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Query q = pm.newQuery(Topic.class,"type == '" + type +"'");
			List<Topic> li = (List<Topic>) q.execute();
			
			/*
			 * Query q = pm.newQuery(Topic.class,"type = typeParam");
			 * q.declareParameters("String typeParam");
			 * List<Topic> li = (List<Topic>) q.execute(type);
			 * */
			
			if(!li.isEmpty()){
				list = new ArrayList<TopicDTO>();
				for(Topic t : li){
					list.add(t.toDTO());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			pm.close();
		}
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<TopicDTO> getTopics(String area, String contentType) throws Exception{
		// TODO Auto-generated method stub
		ArrayList<TopicDTO> list = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Query q = pm.newQuery(Topic.class,"area == '" + area +"' && contentType == '"+contentType+"'");
//			Query q = pm.newQuery(Topic.class,"area == '" + area +"'");
			List<Topic> li = (List<Topic>) q.execute();
			if(!li.isEmpty()){
				list = new ArrayList<TopicDTO>();
				for(Topic t : li){
					list.add(t.toDTO());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally{
			pm.close();
		}
		
		return list;
	}
	
	@Override
	public void updateTopic(TopicDTO topicDTO) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class, topicDTO.getId());
			if(tp != null){
				//chi cap nhat noi dung topic
				tp.updateFromDTO(topicDTO);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteTopic(TopicDTO topicDTO) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class,topicDTO.getId());
			if (tp != null){
				pm.deletePersistent(pm.getObjectById(Topic.class,topicDTO.getId()));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	@Override
	public void deleteTopic(String idTopic) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class,idTopic);
			if (tp != null){
				pm.deletePersistent(pm.getObjectById(Topic.class,idTopic));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
	
	@Override
	public void updateTopic(String idTopic, String changeContent) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Topic tp = pm.getObjectById(Topic.class, idTopic);
			if(tp != null){
				//chi cap nhat noi dung topic
				tp.setContent(changeContent);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
	
}
